package cc.yuxiang.spider;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import jodd.http.HttpRequest;
import jodd.io.NetUtil;
import jodd.jerry.Jerry;
import jodd.jerry.JerryFunction;
import lombok.AllArgsConstructor;
import lombok.val;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
public class SpiderMeizi {
	/** 文件存放路径 */
	private String filePath;
	private final String url = "http://sisirukou194.lofter.com";

	public void spider(int page) {
		Optional.ofNullable(
				HttpRequest.get(url).query("page", Convert.toStr(page), "t", Convert.toStr(System.currentTimeMillis())).send().bodyText())
				.ifPresent(content -> {
					Jerry.jerry(content).$("div.postwrapper div.block").each(new JerryFunction() {
						public Boolean onNode(Jerry $this, int index) {
							val main = $this.$("div.main");
							// 封面链接
//							val fengmian = main.$("div.content div.img a img").attr("src");
							// 跳转链接
							val urlJump = main.$("div.link a").get(1).getAttribute("href");
							Optional.ofNullable(getContentImgs(urlJump)).orElse(Collections.emptyList()).forEach(url -> {
								try {
									NetUtil.downloadFile(url, new File(
											filePath + "/" + CollUtil.join(Arrays.asList(RandomUtil.randomString(10), "jpg"), ".")));
//									log.info("下载图片:{}",url);
								} catch (IOException e) {
									e.printStackTrace();
								}
							});
							return true;
						}
					});
				});
	}

	private List<String> getContentImgs(String urlJump) {
		val $doc = Jerry.jerry(HttpRequest.get(urlJump).send().bodyText());
		List<String> picUrls = new ArrayList<>();
		$doc.$("div.postwrapper div.block div.main div.content div.img").each(new JerryFunction() {
			public Boolean onNode(Jerry $this, int index) {
				picUrls.add($this.$("a").attr("bigimgsrc"));
				return true;
			}
		});
		return picUrls;
	}
}
